package com.example.tao_city.text1;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class MainActivity extends Activity implements View.OnClickListener{
    private ViewPager mViewPager;
    private PagerAdapter madapter;
    private List<View> mlist=new ArrayList<View>();
    private LinearLayout lin1;
    private LinearLayout lin2;
    private LinearLayout lin3;
    private LinearLayout lin4;
    private LinearLayout fm1la,fm1_chaxun,guide,costomer,yinyewangdian,record,refund;
    private View fm1,fm2,fm3,fm4;
    private ImageView mbuttonmag1;
    private ImageView mbuttonmag2;
    private ImageView mbuttonmag3;
    private ImageView mbuttonmag4;
    private ListView fm1list1;
    private List list=new ArrayList();
    private String[] form=new String[]{"img","nsn","usern"};
    private int[] to={R.id.fm1_list_img,R.id.fm1_list_newstitle,R.id.fm1_list_user};
    private ScrollView relative;
    private boolean inten=true;
    private LinearLayout select;
    private TextView iconname;
    private TextView loginpassword;
    private TextView transactionpassword;
    private  TextView gtu,feedback,cmo,aboutus,exit;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initlien();
        fm1List();
        setlistviewheight(fm1list1);

    }

    private void initlien() {
        lin1.setOnClickListener(this);
        lin2.setOnClickListener(this);
        lin3.setOnClickListener(this);
        lin4.setOnClickListener(this);
        fm1la.setOnClickListener(this);
        //该函数对界面滑动改变的监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //当前页面滚动，无论是作为一个以编程方式启动平滑滚动或部分用户发起的触摸滚动时，该方法被调用
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //滚动状态更改时调用。有助于发现用户开始拖动，当寻呼机自动设置到当前页面时，或完全停止/空闲时。
            //我们在这个方法里工作
            @Override
            public void onPageSelected(int position) {
                int item=mViewPager.getCurrentItem();//返回显示页面的参数
                resetimg();//使所有图片变暗
                switch (item){
                    case 0:
                        mbuttonmag1.setImageResource(R.drawable.home_blue);
                        break;
                    case 1:
                        mbuttonmag2.setImageResource(R.drawable.life_blue);
                        break;
                    case 2:
                        mbuttonmag3.setImageResource(R.drawable.my_blue);
                        break;
                    case 3:
                        mbuttonmag4.setImageResource(R.drawable.more_blue);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        lin1=(LinearLayout)findViewById(R.id.lin1);
        lin2=(LinearLayout)findViewById(R.id.lin2);
        lin3=(LinearLayout)findViewById(R.id.lin3);
        lin4=(LinearLayout)findViewById(R.id.lin4);
        mViewPager=(ViewPager)findViewById(R.id.viewPager);
        mbuttonmag1=(ImageView)findViewById(R.id.ib1);
        mbuttonmag2=(ImageView)findViewById(R.id.ib2);
        mbuttonmag3=(ImageView)findViewById(R.id.ib3);
        mbuttonmag4=(ImageView)findViewById(R.id.ib4);
        //把页面转化成视图并添加到数组里
        LayoutInflater mlayout=LayoutInflater.from(this);
        fm1=mlayout.inflate(R.layout.fm1,null);
        fm2=mlayout.inflate(R.layout.fm2,null);
        fm3=mlayout.inflate(R.layout.fm3,null);
        fm4=mlayout.inflate(R.layout.fm4,null);
        mlist.add(fm1);
        mlist.add(fm2);
        mlist.add(fm3);
        mlist.add(fm4);
        //设置适配器
        madapter=new PagerAdapter() {

            @Override
            public Object instantiateItem(ViewGroup container, int position) {//为给定位置创建页面
                View view=mlist.get(position);//获得指定位置页面
                container.addView(view);//把指定页面添加到显示中
                return view;//返回页面
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {//删除指定页面
                container.removeView(mlist.get(position));
            }
            @Override
            public int getCount() {//返回页面的数量
                return mlist.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }
        };
        mViewPager.setAdapter(madapter);//设置适配器

        initotherbuttonn();

    }
    private void initotherbuttonn(){
        fm1la=fm1.findViewById(R.id.fm1_recharge);
        select=fm1.findViewById(R.id.search_et_input);
        fm1_chaxun=fm1.findViewById(R.id.fm1_query);
        guide=fm1.findViewById(R.id.fm1_guide);
        costomer=fm1.findViewById(R.id.fm1_costomer);
        yinyewangdian=fm1.findViewById(R.id.fm1_dot);
        record=fm1.findViewById(R.id.fm1_record);
        refund=fm1.findViewById(R.id.fm1_refund);

        iconname=fm3.findViewById(R.id.fm3_iconname);
        loginpassword=fm3.findViewById(R.id.fm3_lpassword);
        transactionpassword=fm3.findViewById(R.id.fm3_tpassord);
        exit=fm3.findViewById(R.id.fm3_exid);

        gtu=fm4.findViewById(R.id.fm4_gtu);
        feedback=fm4.findViewById(R.id.fm4_feedback);
        cmo=fm4.findViewById(R.id.fm4_cmo);
        aboutus=fm4.findViewById(R.id.fm4_aboutus);

        refund.setOnClickListener(this);
        record.setOnClickListener(this);
        yinyewangdian.setOnClickListener(this);
        exit.setOnClickListener(this);
        costomer.setOnClickListener(this);
        guide.setOnClickListener(this);
        fm1_chaxun.setOnClickListener(this);
        aboutus.setOnClickListener(this);
        cmo.setOnClickListener(this);
        feedback.setOnClickListener(this);
        gtu.setOnClickListener(this);
        transactionpassword.setOnClickListener(this);
        loginpassword.setOnClickListener(this);
        select.setOnClickListener(this);
        iconname.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {//对按键进行监听
        switch (v.getId()){
            case R.id.lin1:
                resetimg();
                mViewPager.setCurrentItem(0);//选择显示页面
                mbuttonmag1.setImageResource(R.drawable.home_blue);//设置图片变亮
                break;
            case R.id.lin2:
                resetimg();
                mViewPager.setCurrentItem(1);
                mbuttonmag2.setImageResource(R.drawable.life_blue);
                break;
            case R.id.lin3:
                resetimg();
                mViewPager.setCurrentItem(2);
                mbuttonmag3.setImageResource(R.drawable.my_blue);
                break;
            case R.id.lin4:
                resetimg();
                mViewPager.setCurrentItem(3);
                mbuttonmag4.setImageResource(R.drawable.more_blue);
                break;
            case R.id.fm1_recharge:
                Intent intent6=new Intent();
                intent6.setClass(MainActivity.this,Fm1_Recharge.class);
                startActivity(intent6);
                break;
            case R.id.search_et_input:
                Intent intent=new Intent();
                if(inten) {
                    intent.setClass(MainActivity.this, SelectNullAction.class);
                    startActivityForResult(intent,20);
                }else {
                    intent.setClass(MainActivity.this, SelectActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.fm3_iconname:
                Intent intent1=new Intent();
                intent1.setClass(MainActivity.this,Personalinformation.class);
                startActivity(intent1);
                break;
            case R.id.fm3_lpassword:
                Intent intent2=new Intent();
                intent2.setClass(MainActivity.this,Loginpassword.class);
                startActivity(intent2);
                break;
            case R.id.fm3_tpassord:
                Intent intent3=new Intent();
                intent3.setClass(MainActivity.this,Transactionpassword.class);
                startActivity(intent3);
                break;
            case R.id.fm4_gtu:
                Intent intent4=new Intent();
                intent4.setClass(MainActivity.this,Fm4_Guide.class);
                startActivity(intent4);
                break;
            case R.id.fm4_feedback:
                Intent intent5=new Intent();
                intent5.setClass(MainActivity.this,Fm4_Opinion.class);
                startActivity(intent5);
                break;
            case R.id.fm4_cmo:
                AlertDialog.Builder builder=new AlertDialog.Builder(this);//新建一个对话框
               // builder.setIcon(R.drawable.icon_phone);//这个是设置在标题左边的图标
                builder.setTitle("联系客服");//设置标题
                builder.setMessage("拨打客服电话10010？");//设置内容
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定按钮
                    @SuppressLint("MissingPermission")
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+"10010"));//拨号设置
                        startActivity(call);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
                break;
            case R.id.fm1_costomer:
                AlertDialog.Builder builder1=new AlertDialog.Builder(this);//新建一个对话框
                // builder1.setIcon(R.drawable.icon_phone);//这个是设置在标题左边的图标
                builder1.setTitle("联系客服");//设置标题
                builder1.setMessage("拨打客服电话10010？");//设置内容
                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定按钮
                    @SuppressLint("MissingPermission")
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+"10010"));//拨号设置
                        startActivity(call);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder1.show();
                break;
            case R.id.fm4_aboutus:
                Intent intent7=new Intent();
                intent7.setClass(this,Fm4_Aboutus.class);
                startActivity(intent7);
                break;
            case R.id.fm1_query:
                Intent intent9=new Intent();
                intent9.setClass(this,Fm1_Chaxun1.class);
                startActivity(intent9);
                break;
            case R.id.fm1_guide:
                Intent intent10=new Intent();
                intent10.setClass(this,Fm4_Guide.class);
                startActivity(intent10);
                break;
            case  R.id.fm3_exid:
                Intent intent11=new Intent();
                intent11.setClass(this,Denlu_Zhuche.class);
                startActivity(intent11);
                break;
            case R.id.fm1_dot:
                Intent intent12=new Intent();
                intent12.setClass(this,Xianluchaxuan.class);
                startActivity(intent12);
                break;
            case R.id.fm1_record:
                Intent intent13=new Intent();
                intent13.setClass(this,Fm1_chongzhijilu.class);
                startActivity(intent13);
                break;
            case R.id.fm1_refund:
                Intent intent14=new Intent();
                intent14.setClass(this,Fm1_tuikuangshengqing.class);
                startActivity(intent14);
                break;
        }
    }
//设置全部图片变黑
    private void resetimg(){
        mbuttonmag1.setImageResource(R.drawable.home_black);
        mbuttonmag2.setImageResource(R.drawable.life_black);
        mbuttonmag3.setImageResource(R.drawable.my_black);
        mbuttonmag4.setImageResource(R.drawable.more_black);
    }

    private void fm1List() {
        fm1list1 = fm1.findViewById(R.id.fm1_listV);

        Map map1 = new HashMap();
        map1.put("img",R.drawable.list_2);
        map1.put("nsn","永州公交司机下班后等候一个多小时 只为等失主");
        map1.put("usern","珠海新闻网");
        list.add(map1);

        Map map2 = new HashMap();
        map2.put("img",R.drawable.list_1);
        map2.put("nsn","因道路施工改造 永州22路等4条公交线路有调整");
        map2.put("usern","珠海新闻网");
        list.add(map2);
        Map map3 = new HashMap();
        map3.put("img",R.drawable.list_2);
        map3.put("nsn","永州公交司机下班后等候一个多小时 只为等失主");
        map3.put("usern","珠海新闻网");
        list.add(map3);

        Map map4 = new HashMap();
        map4.put("img",R.drawable.list_1);
        map4.put("nsn","因道路施工改造 永州22路等4条公交线路有调整");
        map4.put("usern","珠海新闻网");
        list.add(map4);


        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.fm1_list,form,to);
        fm1list1.setAdapter(adapter);
    }
    private void setlistviewheight(ListView listView){
        if(listView==null){
            return ;
        }
        SimpleAdapter simpleAdapter= (SimpleAdapter) listView.getAdapter();
        if(simpleAdapter==null){
            return;
        }
        int height=0;
        for(int i=0;i<simpleAdapter.getCount();i++){
            View listitem=simpleAdapter.getView(i,null,listView);
            listitem.measure(0,0);
            height+=listitem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params=listView.getLayoutParams();
        params.height=height+(listView.getDividerHeight()*(simpleAdapter.getCount()-1));
        listView.setLayoutParams(params);
    }
    @Override
    protected void onActivityResult(int requestcode,int resultcode,Intent intent){
        inten=intent.getExtras().getBoolean("nte");
    }

}
